package com.example.baseadapter_sdk;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseQuickMultipleAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private List<T> data = new ArrayList<>();
    private List<T> list;
    int p;
    int c;
    private int count = 0;


    private static final int HEADTYOE = -1;
    private static final int FOOTTYPE = -2;

    //头布局viewHolder
    private HeaderViewHolder headerViewHolder;
    //脚布局viewHolder
    private FooterViewHolder footerViewHolder;

    private OnItemCLickListener onItemCLickListener;
    private OnItemChildCLickListener onItemChildCLickListener;
    private Context context;
    private List<Integer> ids = new ArrayList<>();



    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        context = recyclerView.getContext();
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (position == 0 && headerViewHolder != null) {
                        return gridLayoutManager.getSpanCount();
                    }

                    if (position == getItemCount() - 1 && footerViewHolder != null) {
                        return gridLayoutManager.getSpanCount();
                    }
                    return 1;
                }
            });


        }
    }

    public void setHeardView(View headerView) {
        if (headerViewHolder == null) {
            LinearLayout linearLayout = new LinearLayout(headerView.getContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            ViewGroup.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            linearLayout.setLayoutParams(layoutParams);

            headerViewHolder = new HeaderViewHolder(linearLayout);
            count++;
            return;
        }

        headerViewHolder.rootView.addView(headerView);

    }

    public void setFoodView(View footerView) {
        if (footerViewHolder == null) {
            LinearLayout linearLayout = new LinearLayout(footerView.getContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            ViewGroup.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            linearLayout.setLayoutParams(layoutParams);

            footerViewHolder = new FooterViewHolder(linearLayout);

            count++;
            return;
        }
        footerViewHolder.rootView.addView(footerView);
    }

    //获取适配器关联数据源
    public List<T> getData() {
        return data;
    }

    public abstract void convert(BaseViewHolder holder, T item);



    //添加子控件监听方法
    protected void addItemChildClick(int... id) {
        for (int i = 0; i < id.length; i++) {
            ids.add(id[i]);
        }
    }

    public void setOnItemCLickListener(OnItemCLickListener onItemCLickListener) {
        this.onItemCLickListener = onItemCLickListener;
    }

    public void setOnItemChildCLickListener(OnItemChildCLickListener onItemChildCLickListener) {
        this.onItemChildCLickListener = onItemChildCLickListener;
    }

    private void bindListener(BaseViewHolder baseViewHolder) {
        if (onItemCLickListener != null) {
            baseViewHolder.itemView.setOnClickListener(v -> {
                onItemCLickListener.onItemClick(this, baseViewHolder.getAdapterPosition() - (headerViewHolder == null ? 0 : 1));
            });
        }
        if (onItemChildCLickListener != null) {
            for (Integer id : ids) {
                baseViewHolder.getView(id).setOnClickListener(v -> {
                    onItemChildCLickListener.onItemChildClick(this, id, baseViewHolder.getAdapterPosition() - (headerViewHolder == null ? 0 : 1));
                });
            }
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HEADTYOE) {
            return headerViewHolder;
        }
        if (viewType == FOOTTYPE) {
            return footerViewHolder;
        }

        View itemView = createItemVIew(viewType,parent);
        BaseViewHolder baseViewHolder = new BaseViewHolder(itemView);
        bindListener(baseViewHolder);


        return baseViewHolder;
    }

    protected abstract View createItemVIew(int viewType,ViewGroup parent);

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder || holder instanceof FooterViewHolder)
            return;

        T item = data.get(position - (headerViewHolder == null ? 0 : 1));
        convert(holder, item);

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && headerViewHolder != null)
            return HEADTYOE;
        else if (position == (getItemCount() - 1) && footerViewHolder != null)
            return FOOTTYPE;

        return getItemType(position);
    }


    @Override
    public int getItemCount() {
        return data.size() + count;
    }

    public abstract int getItemType(int position);

    //添加一条数据源
    public void add(T entity) {
        data.add(entity);
        //局部刷新
        notifyItemInserted(data.size() - 1);
    }

    //添加多条数据源
    public void adds(List<T> list) {
        if (list == null)
            return;
        if (list.size() == 0)
            return;
        int position = data.size() - 1;
        data.addAll(list);
        notifyItemRangeChanged(position, list.size());
    }

    //删除一条数据源
    public void remove(int position) {
        if (position < 0)
            return;
        if (position >= data.size())
            return;
        data.remove(position);
        notifyItemRemoved(position);
    }

    public void setNewInstance(List<T> list) {
        if (data.size() == 0) {
            data.addAll(list);
            notifyDataSetChanged();
            return;
        }
        this.list = list;
        DiffUtil.DiffResult result = DiffUtil.calculateDiff(new BaseQuickDiff<T>(data, this.list));

        result.dispatchUpdatesTo(new ListUpdateCallback() {
            @Override
            public void onInserted(int position, int count) {
                Log.e("ZXY", "onInserted:" + position + "count:" + count);
                data.add(position, (T) list.subList(position, count));
                p = position;
                c = count;
            }

            @Override
            public void onRemoved(int position, int count) {
                Log.e("ZXY", "onRemoved:" + position + "count:" + count);
                int i = position + count - 1;
                for (; i >= position; i--)
                    data.remove(i);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                Log.e("ZXY", "onMoved:");
            }

            @Override
            public void onChanged(int position, int count, @Nullable Object payload) {
                Log.e("ZXY", "onChanged:" + position + "count:" + count);
                onRemoved(position, count);
                onInserted(position, count);
            }
        });

        notifyItemRangeChanged(p, c + (headerViewHolder == null ? 0 : 1));


    }

    //item监听回调
    public interface OnItemCLickListener {
        void onItemClick(RecyclerView.Adapter adapter, int position);
    }

    //itemChild监听回调
    public interface OnItemChildCLickListener {
        void onItemChildClick(RecyclerView.Adapter adapter, int id, int position);
    }

    public Context getContext() {
        return context;
    }
}
